﻿-- Author      : Johannes
-- Create Date : 1/3/2008 9:22:46 PM

function SB_DKP_SearchButton_OnClick()

	if (arg1 == "LeftButton") then
		if (SB_DKP_DEBUG >= 1) then
			SB_DKP:Print("Regular search started.")
		end
		SB_DKP:UISearch()
	elseif (arg1 == "MiddleButton") then
		if (SB_DKP_DEBUG >= 1) then
			SB_DKP:Print("Party/Raid search started.")
		end
		SB_DKP:UIListGroup()
	elseif (arg1 == "RightButton") then
		if (SB_DKP_DEBUG >= 1) then
			SB_DKP:Print("Special search menu unfolding.")
		end
		ToggleDropDownMenu(1, nil, SB_DKP_DropDown, SB_DKP_SearchButton, 0, 0)
	else
		if (SB_DKP_DEBUG >= 1) then
			SB_DKP:Print("Unknown button press... weird.")
		end
	end
	
end

function SB_DKP_SearchBox_OnEnterPressed()
	this:ClearFocus()
	SB_DKP:UISearch()
end

function SB_DKP_SearchBox_OnLoad()
	this:SetTextInsets(0, 8, 0, 0)
end

function SB_DKP_TempFrame_OnLoad()
	SB_DKP:Print("Initiating SB DKP - hold tight.")
	this:RegisterForDrag("LeftButton", "RightButton")
	this:SetMinResize(160, 260);
	SB_DKP:OnLoad()
end

function SB_DKP_TempFrame_OnDragStart()
	if (arg1 == "LeftButton") then
		this:StartMoving();
		this.isMoving = true;
	end
end

function SB_DKP_TempFrame_OnDragStop()
	this:StopMovingOrSizing();
	this.isMoving = false;
end

function SB_DKP_SearchButton_OnLoad()
	this:RegisterForClicks("LeftButtonUp", "RightButtonUp", "MiddleButtonUp");
end

function SB_DKP_DropDown_OnLoad()
-- Table reuse found on WoWWiki.com (http://wowwiki.com/HOWTO:_Use_Tables_Without_Generating_Extra_Garbage) thanks to the author.
-- Dropdown code with help from WoWWiki.com (http://wowwiki.com/HOWTO:_Use_UIDropDownMenu) - again, thanks to the author.
	local MenuData = {}
	
	MenuData.text = "- Your character:"
	MenuData.value = "-yc"
	MenuData.func = nil
		UIDropDownMenu_AddButton(MenuData)
	
	MenuData.text = "Tier 4/5"
	MenuData.value = "tbclo"
	MenuData.func = SB_DKP.DropDown.TierTBCLo
		UIDropDownMenu_AddButton(MenuData)
	
	MenuData.text = "Tier 6"
	MenuData.value = "tbchi"
	MenuData.func = SB_DKP.DropDown.TierTBCHi
		UIDropDownMenu_AddButton(MenuData)
		
	MenuData.text = "- Classes:"
	MenuData.value = "classes"
	MenuData.func = nil
		UIDropDownMenu_AddButton(MenuData)
	
	MenuData.text = "Druid"
	MenuData.value = "druid"
	MenuData.func = SB_DKP.DropDown.Druid
		UIDropDownMenu_AddButton(MenuData)
	
	MenuData.text = "Hunter"
	MenuData.value = "hunter"
	MenuData.func = SB_DKP.DropDown.Rogue
		UIDropDownMenu_AddButton(MenuData)

	MenuData.text = "Mage"
	MenuData.value = "mage"
	MenuData.func = SB_DKP.DropDown.Mage
		UIDropDownMenu_AddButton(MenuData)

	MenuData.text = "Paladin"
	MenuData.value = "paladin"
	MenuData.func = SB_DKP.DropDown.Paladin
		UIDropDownMenu_AddButton(MenuData)

	MenuData.text = "Priest"
	MenuData.value = "priest"
	MenuData.func = SB_DKP.DropDown.Priest
		UIDropDownMenu_AddButton(MenuData)

	MenuData.text = "Rogue"
	MenuData.value = "rogue"
	MenuData.func = SB_DKP.DropDown.Rogue
		UIDropDownMenu_AddButton(MenuData)

	MenuData.text = "Shaman"
	MenuData.value = "shaman"
	MenuData.func = SB_DKP.DropDown.Shaman
		UIDropDownMenu_AddButton(MenuData)

	MenuData.text = "Warlock"
	MenuData.value = "warlock"
	MenuData.func = SB_DKP.DropDown.Warlock
		UIDropDownMenu_AddButton(MenuData)

	MenuData.text = "Warrior"
	MenuData.value = "warrior"
	MenuData.func = SB_DKP.DropDown.Warrior
		UIDropDownMenu_AddButton(MenuData)

	MenuData.text = "- Armor types:"
	MenuData.value = "-ac"
	MenuData.func = nil
		UIDropDownMenu_AddButton(MenuData)
		
	MenuData.text = "Cloth"
	MenuData.value = "cloth"
	MenuData.func = SB_DKP.DropDown.Warrior
		UIDropDownMenu_AddButton(MenuData)

	MenuData.text = "Leather"
	MenuData.value = "leather"
	MenuData.func = SB_DKP.DropDown.Leather
		UIDropDownMenu_AddButton(MenuData)

	MenuData.text = "Mail"
	MenuData.value = "mail"
	MenuData.func = SB_DKP.DropDown.Mail
		UIDropDownMenu_AddButton(MenuData)

	MenuData.text = "Plate"
	MenuData.value = "plate"
	MenuData.func = SB_DKP.DropDown.Plate
		UIDropDownMenu_AddButton(MenuData)

end

function SB_DKP_TempFrame_OnMouseUp()

end

function SB_DKP_TempFrame_OnEvent()
	if (event == "VARIABLES_LOADED") then
	
	elseif (event == "ADDON_LOADED" and arg1 == "SB_DKP") then
		-- Do nothing for now.
	elseif (event == "CHAT_MSG_LOOT") then
    if (SB_DKP_DEBUG > 1) then
      SB_DKP:PrintError("SB_DKP_Frame_OnEvent: loot event: '"..arg1.."'.")
    end
    if (strfind(arg1, " receive") == nil) then -- in the loot event message, look for the fragment " receive". This can encompass both the player " receive " and someone else " received "... maybe I should just search for either one... hm.
		if (SB_DKP_DEBUG > 1) then
			SB_DKP:Print("SB_DKP_Frame_OnEvent: Loot event without ' receive' ignored.")
		end
		return
    end
    SB_DKP_SavedLootEvent = arg1
    local tempFucked = SB_DKP_SavedLootEvent -- Basically useless, but implemented until further notice.
    tempFucked = strtrim(tempFucked) -- trim it... just in case.
    -- Finding the looter.
    local tempLooter = strsub(tempFucked, 0, strfind(tempFucked, " "))
    tempLooter = strtrim(tempLooter)
    if (strlower(tempLooter) == "you") then
      if (SB_DKP_DEBUG >= 1) then
        SB_DKP:Print("SB_DKP_Frame_OnEvent: You were the looter. Changing frame info.")
      end
      tempLooter = UnitName("player")
    end    
    -- Extracting the loot's ItemLink.
    local itemLooted = SB_DKP:ItemObjectCreate(arg1)
    if (SB_DKP_DEBUG > 1) then
      SB_DKP:Print("SB_DKP_Frame_OnEvent: " .. tempLooter .. " looted '" .. itemLooted.Name .. "'(" .. itemLooted.ID .. ").") -- entire ItemLink.
    end
    if (SB_DKP_DEBUG > 1) then
      SB_DKP:Print("SB_DKP_Frame_OnEvent: Currently looted items quality code is " .. itemLooted.Color .. ".")
      if (tempQuality ~= "b048f8") then
        SB_DKP:Print("SB_DKP_Frame_OnEvent: It is NOT epic. If it is, please inform Mazyridon of the HEX color code.")
      end
    end
    -- Insert into the loot dialogue, if relevant.
    if (SB_DKP.Loot.Ignore.Name[itemLooted.Name] == nil) then
		-- Start the dialogue through a neat function... I just haven't thought of a name just yet.
    end
    tinsert(SB_DKP.Loot.Session, {tempLooter, itemLooted, 0, 1}) -- last two indices are price and quantity, respectively.
    SB_DKP.Loot.Counter = SB_DKP.Loot.Counter + 1
  end
end

-- Toggle function for loot price frame.
function SB_DKP_Frame_ButtonPriceFrameOpen_OnClick()
	if (SB_DKP_LootPriceFrame:IsShown()) then
		SB_DKP_LootPriceFrame:Hide()
	else
		SB_DKP_LootPriceFrame:Show()
	end
end

function SB_DKP_LootPrice_ButtonGet_OnClick()
	--put your event handler logic here
end

function SB_DKP_ScrollFrame_OnVerticalScroll()
	FauxScrollFrame_OnVerticalScroll(16, SB_DKP_ScrollFrame_Update);
end

function SB_DKP_ScrollFrame_OnShow()
	SB_DKP_ScrollFrame_Update()
end

function SB_DKP_ScrollFrame_Update()
	-- For an as-of-yet-unknown reason I've had to implement a hack in the update function, faking entries to be printed... damnit!
	if (#(SB_DKP.UI.Current) < 2) then
		tinsert(SB_DKP.UI.Current, {"",""})
	end
	FauxScrollFrame_Update(SB_DKP_ScrollFrame, #(SB_DKP.UI.Current), 1, 16);
	-- #(SB_DKP.UI.Current) is max entries, 9 is number of lines, 16 is pixel height of each line
	if (SB_DKP_DEBUG > 1) then
		SB_DKP:Print("SB_DKP_ScrollFrame_Update: We're at "..FauxScrollFrame_GetOffset(SB_DKP_ScrollFrame));
		SB_DKP:Print("SB_DKP_ScrollFrame_Update: The table contains "..#(SB_DKP.UI.Current).." entries. First entry is ("..SB_DKP.UI.Current[1][1]..","..SB_DKP.UI.Current[1][2]..").")
	end
	local line; -- 1 through 9 of our window to scroll
	local lineplusoffset; -- an index into our data calculated from the scroll offset
	for line=1,9 do
		lineplusoffset = line + FauxScrollFrame_GetOffset(SB_DKP_ScrollFrame);
		if (SB_DKP_DEBUG > 1) then
			SB_DKP:Print("SB_DKP_ScrollFrame_Update: lineplussoffset = "..lineplusoffset..".")
			SB_DKP:Print("SB_DKP_ScrollFrame_Update: #(SB_DKP.UI.Current) = "..#(SB_DKP.UI.Current)..".")
		end
		if (lineplusoffset <= #(SB_DKP.UI.Current)) then
			if (SB_DKP_DEBUG > 1) then
				SB_DKP:Print("Still printing. Right now it's ("..SB_DKP.UI.Current[lineplusoffset][1]..","..SB_DKP.UI.Current[lineplusoffset][2]..").")
			end
			getglobal("SB_DKP_ScrollFrameButton"..line):SetText(SB_DKP.UI.Current[lineplusoffset][1])
			getglobal("SB_DKP_ScrollFrameDKPButton"..line):SetText(SB_DKP.UI.Current[lineplusoffset][2])
			getglobal("SB_DKP_ScrollFrameButton"..line):Show()
			getglobal("SB_DKP_ScrollFrameDKPButton"..line):Show()
		else
			if (SB_DKP_DEBUG > 1) then
				SB_DKP:Print("SB_DKP_ScrollFrame_Update: #(SB_DKP.UI.Current) is less than lineplussoffset. Ending this")
			end
			getglobal("SB_DKP_ScrollFrameButton"..line):Hide()
			getglobal("SB_DKP_ScrollFrameDKPButton"..line):Hide()
		end
	end
end